package com.zh.risk.controller;

import com.alibaba.fastjson.JSONException;
import com.zh.common.aop.SpecialAop;
import com.zh.common.entity.JSONResult;
import com.zh.risk.entity.CheckResult;
import com.zh.risk.service.CallRiskService;
import com.zh.risk.service.RiskMerchTransService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Map;

/**
 * code is far away from bug with the animal protecting
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃代码无BUG！
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 *
 * @Description: 调用风控模型控制器
 * @Author: Liard
 * @Create in: 2020/8/24 9:26
 * @Version: 1.0.1
 */
@Api(value = "调用风控模型控制器", tags = "CallRiskController")
@RestController
@RequestMapping("/call")
@Slf4j
public class CallRiskController {


    @Autowired
    private RiskMerchTransService service;

    @Autowired
    private CallRiskService callRiskService;

    @SpecialAop
    @ApiOperation(value = "ip黑名单与敏感地区检测", tags = "ipIntercept",notes = "ip黑名单与敏感地区检测")
    @PostMapping("/ipIntercept")
    @ResponseBody
    public JSONResult<Object> ipIntercept(@RequestBody String param) {
        JSONResult<Object> result = new JSONResult<>();
        try {
            if (service.getBlackTransList(param))
                result.setData(new CheckResult("该商户存在于ip黑名单或敏感地区中，拒绝交易", false));
            else
                result.setData(new CheckResult("正常商户", true));
            result.setSuccess(true);
            result.setCode(200);
        } catch (JSONException e) {
            log.error(e.getMessage());
        }
        return result;
    }

    @SpecialAop
    @ApiOperation(value = "限制地区检测", tags = "areaIntercept",notes = "限制地区检测")
    @PostMapping("/areaIntercept")
    @ResponseBody
    public JSONResult<Object> areaIntercept(@RequestBody String param) {
        JSONResult<Object> result = new JSONResult<>();
        try {
            Map<String, Object> retMap = service.getBlackAreaList(param);
            if (!(Boolean) retMap.get("success"))
                result.setData(new CheckResult(retMap.get("message").toString(), false));
            else
                result.setData(new CheckResult("正常商户", true));
            result.setSuccess(true);
            result.setCode(200);
        } catch (JSONException e) {
            log.error(e.getMessage());
        }
        return result;
    }

    @SpecialAop
    @ApiOperation(value = "交易风控 ", tags = "transIntercept", notes = "交易风控接口")
    @PostMapping("/transIntercept")
    @ResponseBody
    public JSONResult<Object> transIntercept(@RequestBody String param) {
        JSONResult<Object> result = new JSONResult<>();
        if (callRiskService.checkMrc(param))
            result.setData(new CheckResult("该商户存在于黑名单中，拒绝交易", false));
        JSONResult<Object>  checkResult =  callRiskService.checkTran(param);
        result.setSuccess(true);
        result.setCode(200);
        result.setMessage(checkResult.getMessage());
        result.setData(checkResult.getData());
        return result;
    }


}
